import { setToken, getToken, removeToken } from '@/utils/storage'
const state = {
  tokenInfo: getToken() || {},
  userInfo: {}
}

const mutations = {
  setToken(state, payload) {
    state.tokenInfo = payload
    // 把payload保存到localStorage中
    setToken(payload)
  },
  removeToken(state) {
    state.tokenInfo = ''
    // 删除localStorage
    removeToken()
  },
  setUserInfo(state, payload) {
    state.userInfo = payload
  }
}

const actions = {
  async getUserInfo(context) {
    const res = await this.getUserInfo()
    context.commit('setUserInfo', res.data.data)
  }
}

const getters = {}

export default {
  state,
  mutations,
  actions,
  getters,
  // 开启命名空间
  namespaced: true
}
